public class Test {

    /**
     * 力扣（将x减到0的最小操作数）:滑动窗口
     * */
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int a:nums)sum+=a;
        int target = sum-x;
        if(target<0)return -1;
        int ret=-1;
        int n=nums.length;
        for(int left=0,right=0,tmp=0;right<n;right++){
            tmp+=nums[right];//进窗口
            while(tmp>target){//判断
                tmp-=nums[left++];
            }
            if(tmp==target){
                ret=Math.max(ret,right-left+1);
            }
        }
        if(ret==-1)return ret;
        else return n-ret;
    }

    public static void main(String[] args) {
        int[] nums = {1,1,4,2,3};
        System.out.println(new Test().minOperations(nums, 5));
    }
}
